﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Concurrent;

namespace BaseSimpleAPI
{
    /// <summary>
    /// 网络调用上下文
    /// </summary>
    public class NetCallContext
    {
        private NetCallContext() { }

        /// <summary>
        /// 我的小布兜
        /// </summary>
        private ConcurrentDictionary<string, object> _Tag = new ConcurrentDictionary<string, object>();

        /// <summary>
        /// 当前对象
        /// </summary>
        public static NetCallContext Current
        {
            get { return (NetCallContext)CallContext.HostContext; }
        }

        /// <summary>
        /// 新的实例
        /// </summary>
        /// <returns></returns>
        public static NetCallContext NewCurrent()
        {
            NetCallContext context = new NetCallContext(); ;
            CallContext.HostContext = context;
            return context;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        public void SetData<T>(T t)
        {
            _Tag[typeof(T).FullName] = t;
        }

        /// <summary>
        /// 尝试获取数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T GetData<T>()
        {
            var type_name = typeof(T).FullName;

            if (_Tag.TryGetValue(type_name, out object obj) && obj is T)
                return (T)obj;

            return default(T);
        }

        /// <summary>
        /// 当前用户
        /// </summary>
        public long CurrentUserId
        {
            get
            {
                return 0;
            }
        }

        public static Func<Token, bool> CheckTokenFunc { get; set; }
    }
}
